package com.shujia.MapReduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Demo02ClazzCnt {
    // Map端
    public static class MyMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
        @Override
        protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            // 提取班级
            // 1500100001,施笑槐,22,女,文科六班
            String clazz = value.toString().split(",")[4];
            context.write(new Text(clazz), new IntWritable(1));
        }
    }

    // Reduce端
    public static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
            int cnt = 0;
            for (IntWritable value : values) {
                cnt += value.get();
            }
            context.write(key, new IntWritable(cnt));
        }
    }

    // Driver程序，主要负责配置及提交任务
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://master:9000");
        // 创建一个MapReduce的Job
        Job job = Job.getInstance(conf);
        // 配置任务
        // 设置任务运行的名字
        job.setJobName("Demo02ClazzCnt");
        // 设置任务运行哪一个类
        job.setJarByClass(Demo02ClazzCnt.class);
        // 设置Reduce的数量，默认1，最终生成文件的数量同Reduce的数量一致
        job.setNumReduceTasks(12);
        // 使用自定义的分区类
        job.setPartitionerClass(ClazzPartitioner.class);

        // 配置Map端
        // 指定Map任务运行哪一个类
        job.setMapperClass(MyMapper.class);
        // 配置Map端输出的Key的类型
        job.setMapOutputKeyClass(Text.class);
        // 配置Map端输出的Value的类型
        job.setMapOutputValueClass(IntWritable.class);

        // 配置Reduce端
        // 指定Reduce任务运行哪一个类
        job.setReducerClass(MyReducer.class);
        // 配置Reduce端输出的Key的类型
        job.setOutputKeyClass(Text.class);
        // 配置Reduce端输出的Value的类型
        job.setOutputValueClass(IntWritable.class);

        // 配置输入输出路径

        FileInputFormat.addInputPath(job, new Path("/data/stu/input"));

        Path path = new Path("/data/stu/output");
        FileSystem fs = FileSystem.get(conf);
        // 判断输出路径是否存在，存在即删除
        if (fs.exists(path)) {
            fs.delete(path, true);
        }
        // 输出路径已存在会报错
        FileOutputFormat.setOutputPath(job, path);


        // 等待任务运行完成
        job.waitForCompletion(true);

    }
    /*
     * hdfs dfs -mkdir -p /data/stu/input
     * hdfs dfs -put students.txt /data/stu/input
     * hadoop jar Hadoop-1.0.jar com.shujia.MapReduce.Demo02ClazzCnt
     */

}

class ClazzPartitioner extends Partitioner<Text, IntWritable> {

    @Override
    public int getPartition(Text key, IntWritable value, int numReduces) {
        String clazz = key.toString();
        switch (clazz) {
            case "文科一班":
                return 0;
            case "文科二班":
                return 1;
            case "文科三班":
                return 2;
            case "文科四班":
                return 3;
            case "文科五班":
                return 4;
            case "文科六班":
                return 5;
            case "理科一班":
                return 6;
            case "理科二班":
                return 7;
            case "理科三班":
                return 8;
            case "理科四班":
                return 9;
            case "理科五班":
                return 10;
            case "理科六班":
                return 11;

        }
        return 0;
    }
}
